package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunPost;
import com.ruoyi.quanzi.service.ISunPostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 帖子管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/system/sunpost")
public class SunPostController extends BaseController
{
    @Autowired
    private ISunPostService sunPostService;

    /**
     * 查询帖子列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunPost sunPost)
    {
        startPage();
        List<SunPost> list = sunPostService.selectSunPostList(sunPost);
        return getDataTable(list);
    }

    /**
     * 导出帖子列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @Log(title = "帖子管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunPost sunPost)
    {
        List<SunPost> list = sunPostService.selectSunPostList(sunPost);
        ExcelUtil<SunPost> util = new ExcelUtil<SunPost>(SunPost.class);
        util.exportExcel(response, list, "帖子数据");
    }

    /**
     * 获取帖子详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId)
    {
        return AjaxResult.success(sunPostService.selectSunPostById(postId));
    }

    /**
     * 删除帖子
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds)
    {
        return toAjax(sunPostService.deleteSunPostByIds(postIds));
    }

    /**
    /**
     * 修改帖子状态
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "帖子状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SunPost sunPost)
    {
        return toAjax(sunPostService.updateSunPost(sunPost));
    }

    /**
     * 审核帖子
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "帖子审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult auditPost(@RequestBody SunPost sunPost)
    {
        // 设置审核时间和审核人
        sunPost.setUpdateTime(new java.util.Date());
        sunPost.setUpdateBy(getUsername());
        return toAjax(sunPostService.updateSunPost(sunPost));
    }

    /**
     * 批量审核帖子
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "批量审核帖子", businessType = BusinessType.UPDATE)
    @PutMapping("/batchAudit")
    public AjaxResult batchAudit(@RequestBody java.util.Map<String, Object> params)
    {
        Long[] postIds = (Long[]) params.get("postIds");
        Integer status = (Integer) params.get("status");
        String remark = (String) params.get("remark");
        
        int result = 0;
        for (Long postId : postIds) {
            SunPost sunPost = new SunPost();
            sunPost.setPostId(postId);
            sunPost.setStatus(status);
            sunPost.setRemark(remark);
            sunPost.setUpdateTime(new java.util.Date());
            sunPost.setUpdateBy(getUsername());
            result += sunPostService.updateSunPost(sunPost);
        }
        
        return toAjax(result);
    }

    /**
     * 设置/取消精华
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "帖子精华", businessType = BusinessType.UPDATE)
    @PutMapping("/toggleFeatured")
    public AjaxResult toggleFeatured(@RequestBody SunPost sunPost)
    {
        return toAjax(sunPostService.updateSunPost(sunPost));
    }

    /**
     * 设置/取消置顶
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @Log(title = "帖子置顶", businessType = BusinessType.UPDATE)
    @PutMapping("/toggleSticky")
    public AjaxResult toggleSticky(@RequestBody SunPost sunPost)
    {
        return toAjax(sunPostService.updateSunPost(sunPost));
    }

    /**
     * 查询帖子评论列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping("/{postId}/comments")
    public TableDataInfo listComments(@PathVariable("postId") Long postId)
    {
        startPage();
        // 这里需要根据实际的评论实体和服务来实现
        // List<SunComment> list = sunCommentService.selectCommentsByPostId(postId);
        // return getDataTable(list);
        return getDataTable(null); // 临时返回，需要实现评论相关功能
    }

    /**
     * 删除帖子评论
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @Log(title = "删除评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/comment/{commentId}")
    public AjaxResult delComment(@PathVariable("commentId") Long commentId)
    {
        // 这里需要根据实际的评论服务来实现
        // return toAjax(sunCommentService.deleteSunCommentByCommentId(commentId));
        return AjaxResult.success(); // 临时返回，需要实现评论相关功能
    }
}